package com.lysj.merchant.management.service;


import com.lysj.admin.utils.constant.CommonConstant;
import com.lysj.admin.utils.spring.foundation.base.BaseService;
import com.lysj.admin.utils.web.ParamUtil;
import com.lysj.admin.utils.web.Resp;
import com.lysj.merchant.management.domain.Permission;
import com.lysj.merchant.management.domain.Role;
import com.lysj.merchant.management.domain.RolePermission;
import com.lysj.merchant.management.repository.PermissionRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;
import java.util.List;

/**
 * @author zk
 * @description 权限表服务层
 * @create 2018-07-25 15:32:54
 **/
@Slf4j
@Service
@Transactional
public class PermissionService implements BaseService<Permission> {

    @Resource
    private PermissionRepository permissionRepository;

    @Resource
    private RoleService roleService;

    @Resource
    private RolePermissionService rolePermissionService;

    @PersistenceContext
    private EntityManager em;

    @Override
    public PermissionRepository getRepository() {
        return permissionRepository;
    }

    public List<Permission> findByUserId(String userId) {
        StringBuilder sb = new StringBuilder();
        sb.append("select distinct p from User u" +
                " ,UserRole ur ,RolePermission rp ,Permission p where u.id = ur.userId and ur.roleId = rp.roleId and p.id = rp.permissionId" +
                " and u.id = ? and p.delFlag =");
        sb.append(CommonConstant.NORMAL_FLAG);
        sb.append("  order by p.sortOrder");
        Query query = em.createQuery(sb.toString(), Permission.class).setParameter(1, userId);
        List<Permission> list = query.getResultList();
        return list;
    }

    public List<Permission> findByUserIdAndType(String userId) {
        StringBuilder sb = new StringBuilder();
        sb.append("select distinct p from User u" +
                " ,UserRole ur ,RolePermission rp ,Permission p where u.id = ur.userId and ur.roleId = rp.roleId and p.id = rp.permissionId" +
                " and u.id = ? and p.delFlag =");
        sb.append(CommonConstant.NORMAL_FLAG);
        sb.append(" and p.type = 1");
        sb.append("  order by p.sortOrder");
        Query query = em.createQuery(sb.toString(), Permission.class).setParameter(1, userId);
        List<Permission> list = query.getResultList();
        return list;
    }

    public List<Permission> findByRoleId(String roleId) {
        StringBuilder sb = new StringBuilder();
        sb.append("select p from RolePermission rp ,Permission p where p.id = rp.permissionId and rp.roleId = ? and p.delFlag =");
        sb.append(CommonConstant.NORMAL_FLAG);
        sb.append("  order by p.sortOrder");
        Query query = em.createQuery(sb.toString(), Permission.class).setParameter(1, roleId);
        List<Permission> list = query.getResultList();
        return list;
    }

    public List<Permission> findByLevelOrderBySortOrderDesc(Integer level) {
        return permissionRepository.findByLevelOrderBySortOrder(level);
    }


    public List<Permission> findByParentIdOrderBySortOrder(String parentId) {
        return permissionRepository.findByParentIdOrderBySortOrder(parentId);
    }


    /**
     * @author Created by wtl on 2019/3/13 15:51
     * @Description 添加权限
     */
    public String add(Permission entity) {
        update(entity);
        return "添加成功";
    }

}